-- 周末好礼定期推送
local mis_uu = require('modules.commhm.utils')
local uu = mis_uu
local mis_timer = require('modules.commhm.ngxTimerWrap')
local os_date = os.date
local math_floor = math.floor

local ngx = ngx

local mis_log = nil
local mis_config = nil
local mis_data = nil
local mis_common = nil
local mis_bridge = nil
local mis_calcu = nil
local t_retCode = nil

--------------------------------------------------------------
local mis_weekendPush = {
	s_name = string.match(debug.getinfo(1).short_src, '.*/(.*).lua');	-- 模块名称
}
local this = mis_weekendPush

--------------------------------------------------------------
-- 说明：定期发推送
local function send_push_loop()
	local n_code, s_desc
	local n_now = mis_data.offset_now_v2(mis_data.t_weekend_gift_v2)
	local n_hour = tonumber(os_date("%H", n_now))
	local n_min = tonumber(os_date("%M", n_now))
	local n_weekday = tonumber(os_date("%w", n_now))
	local s_nowMin = os_date("%Y%m%d %H:%M", n_now)
	local s_actType = mis_config.s_mobpush_flag_weekend_gift_v2
	local t_conf = mis_data.t_weekend_gift_v2.t_conf
	local t_push = mis_data.t_weekend_gift_v2.t_conf.t_push
	local n_prepareSend, n_prepareHour, n_prepareMin, n_prepareWeekday
	local n_sendWeekday, n_sendTime

	repeat
		if not t_conf or mis_data.t_weekend_gift_v2.n_status ~= mis_config.t_workStatus.n_ENABLE then
			mis_log.day('weekendPush', 'weekend gift not enable! config: ' .. uu.to_str(t_conf))
			break
		end

		if t_conf.n_startTime and n_now < t_conf.n_startTime then
			mis_log.day('weekendPush', 'weekend gift not start!')
			break
		end

		if t_conf.n_endTime and t_conf.n_endTime < n_now then
			mis_log.day('weekendPush', 'weekend gift out of time!')
			break
		end

		if t_push.n_send ~= 1 then
			mis_log.day('weekendPush', 'weekend gift push send closed by config! send: ' .. uu.to_str(t_push.n_send))
			break
		end

		-- 计算预发送时间
		n_prepareSend = mis_common.date_to_unix(
			string.format("%s %02d:%02d:00", os_date("%Y-%m-%d", n_now), 
			t_push.t_send_time.hour, t_push.t_send_time.min))
		n_prepareSend = n_prepareSend - t_push.n_prepare_sec
		n_prepareHour = tonumber(os_date("%H", n_prepareSend))
		n_prepareMin = tonumber(os_date("%M", n_prepareSend))
		n_prepareWeekday = tonumber(os_date("%w", n_prepareSend))

		n_sendWeekday = t_push.n_send_weekday - (n_weekday - n_prepareWeekday)

		if (n_sendWeekday % 7) ~= (n_weekday % 7) then
			mis_log.day('weekendPush', 'weekend gift weekday not match: config weekday: ' 
				.. uu.to_str(t_push.n_send_weekday) .. ", prepare weekday: " 
				.. n_prepareWeekday .. ", send weekday: " .. n_sendWeekday 
				.. ", n_prepareSend: " .. n_prepareSend)
			break
		end

		if n_prepareHour ~= n_hour or n_prepareMin ~= n_min then
			mis_log.day('weekendPush', 'weekend gift push time not match! now: ' .. n_now .. ", prepareSend " .. n_prepareSend .. ", n_prepareHour " .. n_prepareHour .. ", n_prepareMin " .. n_prepareMin)
			break
		end

		-- if not t_push.s_title or not t_push.s_content then
		-- 	mis_log.day('weekendPush', 'weekend gift push content error: ' .. uu.to_str(t_push))
		-- 	break
		-- end

		-- 防止多发
		if ngx.weekendPushLastSendTime ~= s_nowMin then
			ngx.weekendPushLastSendTime = s_nowMin
			-- 发送
			n_sendTime = math_floor((ngx.now() + t_push.n_prepare_sec) * 1000)
			n_code, s_desc = mis_bridge.modpush_send_batch(s_actType, n_sendTime)
			if n_code ~= t_retCode.n_OK then
				mis_log.day('weekendPush', 'send error! code: ' .. n_code .. ", msg: ".. uu.to_str(s_desc))
				break
			else
				mis_log.day('weekendPush', 'send weekend gift push ok!')
			end
		end
	until true

	mis_log.day('weekendPush', 'weekend gift push loop! now: ' .. n_now)

	mis_timer:new():ngx_timer_at(mis_config.n_weekendPushInterval, send_push_loop)
end

--------------------------------------------------------------
-- 说明：模块包含
-- 参数：模块表
this.modules_include = function(t_moddules)
	mis_log = t_moddules.mis_log
	mis_config = t_moddules.mis_config
	mis_data = t_moddules.mis_data
	mis_common = t_moddules.mis_common
	mis_bridge = t_moddules.mis_bridge
	mis_calcu = t_moddules.mis_calcu
	t_retCode = mis_config.t_retCode
end

--------------------------------------------------------------
-- 说明：load级初始化
this.init_load = function()
	mis_log.day('weekendPush', 'init_load! worker ' .. ngx.worker.id())
	if mis_calcu.is_master_server() == true
	or mis_config.n_debug == 1 then
		-- 开启循环
		if ngx.worker.id() == 0 then
			local _, s_err = mis_timer:new():ngx_timer_at(mis_config.n_weekendPushInterval, send_push_loop)
			if s_err then
				return t_retCode.n_SYS_ERR, 'start ngx.timer.every failed, ' .. s_err
			else
				mis_log.day('weekendPush', 'start!')
			end
		end
	end
	return t_retCode.n_OK
end

return this